package com.andorid.uit;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore.Images;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class ImageListActivity extends ListActivity {
    /** Called when the activity is first created. */
    private Cursor myCursor;
    private List<Image> listImage ;
    private int itemsPerPage = 5;
	private boolean loadingMore = false;
    private ListView listview;
    private Handler handler;
    private ImageListAdapter adapter;
    private int index=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        listview =getListView();
        listview.setChoiceMode(2);
		
        listImage = new ArrayList<Image>();
        // get all cursor
        myCursor=this.managedQuery(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        startManagingCursor(myCursor);
        //add footview
        View footerView = ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listfooter, null, false);
        listview.addFooterView(footerView);


      	 adapter = new ImageListAdapter(this,listImage);
    	for (index=0; index < itemsPerPage; index++) {		
			myCursor.moveToPosition(index);
			//Fill the item with some bogus information
            String imageName = myCursor.getString(myCursor.getColumnIndexOrThrow(Images.Media.DISPLAY_NAME));
            String imageSize= myCursor.getString(myCursor.getColumnIndexOrThrow(Images.Media.SIZE));
            String imageIcon= myCursor.getString(myCursor.getColumnIndexOrThrow(Images.Media.DATA));
            listImage.add(new Image(imageName, imageSize, imageIcon));         	
            Log.d("1","add thnh cong");
			
	}
        // event scroll
        listview.setOnScrollListener(new OnScrollListener() {
			
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub
				
			}
			
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub
				//what is the bottom iten that is visible
				int lastInScreen = firstVisibleItem + visibleItemCount;				
   	        // listImage = new ArrayList<Image>();   	    	

				//is the bottom item visible & not loading more already ? Load more !
				if((lastInScreen == totalItemCount) && !(loadingMore)){			
				/*	loadingMore = true;

					for (int i=0; i < itemsPerPage; i++) {		
	    				myCursor.moveToNext();
	    			//Fill the item with some bogus information
	    	            String imageName = myCursor.getString(myCursor.getColumnIndexOrThrow(Images.Media.DISPLAY_NAME));
	    	            String imageSize= myCursor.getString(myCursor.getColumnIndexOrThrow(Images.Media.SIZE));
	    	            String imageIcon= myCursor.getString(myCursor.getColumnIndexOrThrow(Images.Media.DATA));
	    	            listImage.add(new Image(imageName, imageSize, imageIcon));         	
	    	            Log.d("1","add thnh cong");
	    				
	    			}}
				adapter.notifyDataSetChanged();*/
					//Load the first 15 items
				handler= new Handler();
				Thread thread =  new Thread(null, BagroundtThread);
			    thread.start();
				}
			
			}
        });
    }
        
     private Runnable BagroundtThread = new Runnable()
        {
        	@Override
        	public void run() {
        		// TODO Auto-generated method stub

    			//Set flag so we cant load new items 2 at the same time
    			loadingMore = true;
    			//listImage= new ArrayList<Image>();
    	    	for (int i=index; i < index+itemsPerPage; i++) {		
    				myCursor.moveToPosition(i);
    				//Fill the item with some bogus information
    	            String imageName = myCursor.getString(myCursor.getColumnIndexOrThrow(Images.Media.DISPLAY_NAME));
    	            String imageSize= myCursor.getString(myCursor.getColumnIndexOrThrow(Images.Media.SIZE));
    	            String imageIcon= myCursor.getString(myCursor.getColumnIndexOrThrow(Images.Media.DATA));
    	            listImage.add(new Image(imageName, imageSize, imageIcon));         	
    	            Log.d("0","add thnh cong");
    			}
    	    	adapter.notifyDataSetChanged();

    			//Done! now continue on the UI thread
    	    	handler.post(updateUI);
        	}
        };
        private Runnable updateUI = new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				//loadingMore = false;
				doupdate();
			}
		};
        private void doupdate()
        {    	    	
   		setListAdapter(adapter);
         
		
        }

        //add item in list
     /*   while (myCursor.moveToNext())
        {
            String imageName = myCursor.getString(myCursor.getColumnIndexOrThrow(Images.Media.DISPLAY_NAME));
            String imageSize= myCursor.getString(myCursor.getColumnIndexOrThrow(Images.Media.SIZE));
            String imageIcon= myCursor.getString(myCursor.getColumnIndexOrThrow(Images.Media.DATA));
            listImage.add(new Image(imageName, imageSize, imageIcon));
        }
             // event click item 
        listview.setOnItemClickListener(new OnItemClickListener() 
        {
        	@Override
        	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
        			long arg3) {
        		// TODO Auto-generated method stub
        		ShowToast(listImage.get(arg2).getName());
        	}
		});

    }
    public void ShowToast(String message)
    {
    	Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    	
    }*/
}  